/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Parser;
import Arbori.*;
import Functii.*;
import Operatori.*;
import java.util.ArrayList;

/**
 *
 * @author AlinT
 */
public class TreeInsert {

        ArboreFunctional a;
        ArrayList<Nod> nodList=new ArrayList<Nod>();

        /**
         * Functia ce adauga intr-o lista de noduri nodurile corespunzatoare
         * contorul reprezentand numarul de fii pe care poate sa ii aiba un nod
         * @param nod nodul ce va fi adaugat in lista de noduri
         * @param k pozitia la care va adaugat nodul in lista
         */
    public void addInNodList(String nod, int k){

        Nod nodCurent;

        if(nod.equals("+")) {nodCurent = new Plus(); nodList.add(k, new Plus());}
        else if(nod.equals("-")) {nodCurent = new Minus(); nodCurent.setfStang(new Constanta(0));nodList.add(k, nodCurent);}
        else if(nod.equals("*")) {nodCurent = new Produs(); nodList.add(k, nodCurent);}
        else if(nod.equals("/")) {nodCurent = new Cat(); nodList.add(k, nodCurent);}
        else if(nod.equals("sin")) {nodCurent = new Sin(); nodCurent.setContor(1); nodList.add(k, nodCurent);}
        else if(nod.equals("cos")) {nodCurent = new Cos(); nodCurent.setContor(1); nodList.add(k, nodCurent);}
        else if(nod.equals("tg")) {nodCurent = new Tg(); nodCurent.setContor(1); nodList.add(k, nodCurent);}
        else if(nod.equals("ctg")) {nodCurent = new Ctg(); nodCurent.setContor(1); nodList.add(k, nodCurent);}
        else if(nod.equals("ARCSIN")) {nodCurent = new Arcsin(); nodCurent.setContor(1); nodList.add(k, nodCurent);}
        else if(nod.equals("ARCCOS")) {nodCurent = new Arccos(); nodCurent.setContor(1); nodList.add(k, nodCurent);}
        else if(nod.equals("ARCTG")) {nodCurent = new Arctg(); nodCurent.setContor(1); nodList.add(k, nodCurent);}
        else if(nod.equals("ARCCTG")) {nodCurent = new Arcctg(); nodCurent.setContor(1); nodList.add(k, nodCurent);}
        else if(nod.equals("EXP")) {nodCurent = new Exp(); nodCurent.setContor(1); nodList.add(k, nodCurent);}
        else if(nod.equals("log")) {nodCurent = new Log(); nodCurent.setContor(1); nodList.add(k, nodCurent);}
        else if(nod.equals("pow")) {nodCurent = new Putere(); nodCurent.setContor(1); nodList.add(k, nodCurent);}
        else if(nod.equals("sqrt")) {nodCurent = new Radical(); nodCurent.setContor(1); nodList.add(k, nodCurent);}
        else if(nod.equals("x")) {nodCurent = new Necunoscuta(); nodCurent.setContor(2); nodList.add(k, nodCurent);}
        else try{
                 nodCurent = new Constanta(Integer.parseInt(nod)); nodCurent.setContor(2); nodList.add(k, nodCurent);
                }catch(NumberFormatException e){
                 System.err.println(e.getMessage());
                }
    }

    /**
     * Crearea arborelui functional
     * @param s vectorul transformat in forma poloneza
     * @return arborele functional corespunzator expresiei
     */
    public ArboreFunctional createTree(ArrayList<String> s){

        int l=s.size();
        String rad=s.get(l-1);
 
        addInNodList(rad, 0);

        a=new ArboreFunctional(nodList.get(0));

        int i=l-2, k=0; String nod;
        while(i>=0){

                k++; nod=s.get(i);
                addInNodList(nod, k);

                int j=k-1, q=0;
                while(j>=0){
                    if (nodList.get(j).getContor() == 2) j--;
                         else {q=j; break;}
                }

                if (nodList.get(q).getContor() == 1){
                    nodList.get(q).setfStang(nodList.get(k));
                    nodList.get(q).setContor(2);
                }
                if (nodList.get(q).getContor() == 0){
                    nodList.get(q).setfDrept(nodList.get(k));
                    nodList.get(q).setContor(1);
                }
                i--;

        }
        return a;
    }
}
